#!/bin/bash
#libstardust
#Sigmund Berglund, apr 2014
#GPL

case $1 in ""|-h|--help)
	echo '
This box shows two button (Yes / No), one question-icon, and textstring(s)
markup/span is valid

usage :
 box_yesno [options] HEADING "textstring1" "textstring2" "..."

 HEADING is the window title
 
 options:
   --ok-cancel        : ok/cancel dialog
   --yes-label <text> : yes button label
   --no-label <text>  : no button label
   --yes-first        : show yes button first
   --no-first)        : show no button first
   --error            : error icon (window)
   --info             : info icon (window)
   --warning          : warning icon (window)
   --no-icon <icon>   : set no button icon
   --yes-icon <icon>  : set yes button icon
   --image-icon <icon>: set dialog icon
   --extra-button <text> : extra button
   --extra-icon <icon>   : extra button icon

 Return values:
   yes   = 0
   no    = 1
   extra = 2
   other = 255'
	exit ;;
esac

export TEXTDOMAIN=libstardust
export OUTPUT_CHARSET=UTF-8

YES_LABEL=$(gettext 'Yes')
NO_LABEL=$(gettext 'No')
NO_ICON=no
YES_ICON=yes
ICON="dialog-info.svg"
IMAGE_FILE="dialog-question.svg"

## options:
for i in "$@" ; do
	case $1 in
		--yes-label) YES_LABEL="$2" ; shift 2 ;;
		--no-label)  NO_LABEL="$2"  ; shift 2 ;;
		--yes-first) yes_first=1 ; shift ;;
		--no-first)  yes_first=  ; shift ;;
		--error)   ICON="dialog-error.svg"   ; IMAGE_FILE=$ICON ; shift ;;
		--info)    ICON="dialog-info.svg"    ; IMAGE_FILE=$ICON ; shift ;;
		--warning) ICON="dialog-warning.svg" ; IMAGE_FILE=$ICON ; shift ;;
		--no-icon)   NO_ICON="$2"    ; shift 2 ;;
		--yes-icon)  YES_ICON="$2"   ; shift 2 ;;
		--image-icon)IMAGE_FILE="$2" ; shift 2 ;;
		--ok-cancel)
			NO_ICON=cancel
			YES_ICON=ok
			yes_first=1
			YES_LABEL=$(gettext 'OK')
			NO_LABEL=$(gettext 'Cancel')
			shift
			;;
		--extra-button)
			EXTRA=1
			EXTRA_LABEL="$2"
			shift 2
			;;
		--extra-icon)
			EXTRA_ICON="$2"
			shift 2
			;;
		*) break ;; #no more options...
	esac
done

WINDOW_ICON="/usr/share/pixmaps/puppy/${ICON}"
IMAGE=/usr/share/pixmaps/puppy/${IMAGE_FILE}

NO_BUTTON='
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon ${NO_ICON}`"'
      <label>" '${NO_LABEL}' "</label>
      <action>EXIT:no</action>
    </button>
'
YES_BUTTON='
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon ${YES_ICON}`"'
      <label>" '${YES_LABEL}' "</label>
      <action>EXIT:yes</action>
    </button>
'

if [ $EXTRA ] ; then
	if [ "$EXTRA_ICON" ] ; then
		EI="`/usr/lib/gtkdialog/xml_button-icon ${EXTRA_ICON}`"
	fi
	EXTRA_BUTTON='
    <button>
      '${EI}'
      <label>" '${EXTRA_LABEL}' "</label>
      <action>EXIT:extra</action>
    </button>'
fi

if [ $yes_first ] ; then
	BUTTONS="$YES_BUTTON
$NO_BUTTON
$EXTRA_BUTTON"
else
	BUTTONS="$NO_BUTTON
$YES_BUTTON
$EXTRA_BUTTON"
fi

HEADING="$1"

S='
<window title="'"${HEADING}"'" image-name="'${WINDOW_ICON}'">
   <vbox space-expand="false" space-fill="false">
      <hbox height-request="10" space-expand="true" space-fill="true"><text><label>" "</label></text></hbox>
      <hbox>
         <vbox>
           <pixmap>
             <input file>'${IMAGE}'</input>
             <height>56</height>
             <width>56</width>
           </pixmap>
         </vbox>
         <vbox>'
            [ "$2" ] && S=$S'<text xalign="0" use-markup="true"><label>"'$2'"</label></text>'
            [ "$3" ] && S=$S'<text xalign="0" use-markup="true"><label>"'$3'"</label></text>'
            [ "$4" ] && S=$S'<text xalign="0" use-markup="true"><label>"'$4'"</label></text>'
            [ "$5" ] && S=$S'<text xalign="0" use-markup="true"><label>"'$5'"</label></text>'
            [ "$6" ] && S=$S'<text xalign="0" use-markup="true"><label>"'$6'"</label></text>'
            [ "$7" ] && S=$S'<text xalign="0" use-markup="true"><label>"'$7'"</label></text>'
            [ "$8" ] && S=$S'<text xalign="0" use-markup="true"><label>"'$8'"</label></text>'
S=$S'
            <text xalign="0"><label>" "</label></text>
         </vbox>
      </hbox>
      <hbox space-expand="false" space-fill="false">
         '"${BUTTONS}"'
      </hbox>
   </vbox>
</window>'

export box_yesno="$S"

r="$(gtkdialog --center -p box_yesno)"
eval "$r"

if [ "${BASH_SOURCE[0]}" = "$0" ] ; then
	#not being sourced..
	case "$EXIT" in
		yes)   exit 0 ;;
		no)    exit 1 ;;
		extra) exit 2 ;;
		*)     exit 255 ;;
	esac
fi

### END ###
